package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bean.Goods;
import com.bean.Seckill;
import com.bean.User;
import com.dao.GoodsMapper;
import com.dao.UserMapper;
import com.service.IUserService;
import com.utils.SaltUtis;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WxrStart
 * @create 2022-05-17 16:58
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    public UserMapper userMapper;

    /**
     *  注册User用户
     * @return
     */
    @Override
    public Integer addUser(User user) {
        //生成随机盐
        String salt = SaltUtis.getSalt(8);
        //将随机盐保存到数据库
        user.setSalt(salt);
        //将密码进行md5+salt+hash散列
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);
        user.setPassword(md5Hash.toHex());
        return userMapper.insert(user);
    }

    /**
     * 根据邮箱查找用户
     * @param email 用户邮箱
     * @return
     */
    @Override
    public User findByEmail(String email) {
        QueryWrapper<User> queryWrapper=new QueryWrapper();
        queryWrapper.eq("email",email);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 根据邮箱和密码查找用户
     * @param email 用户邮箱
     * @param password 用户密码
     * @return
     */
    @Override
    public User findByEmailAndPassword(String email, String password) {
        QueryWrapper<User> queryWrapper=new QueryWrapper();
        queryWrapper.eq("email",email);
        queryWrapper.eq("password",password);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 根据秒杀id查找用户
     * @param seckill
     * @return
     */
    @Override
    public User findBySeckill(Seckill seckill) {
        Integer seckillId = seckill.getSeckillId();
      return   userMapper.getUserBySeckillId(seckillId);

    }

    /**
     * 根据用户Id修改用户
     * @param userId
     * @return
     */
    @Override
    public Integer updateByUserId(String nickName,String phone,String address,Integer userId) {
       return userMapper.updateByUserId(nickName,phone,address,userId);
    }


}
